package com.panshi.tools;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

import java.util.Random;

public class SMStools {

	private final  static String  SMS_URL="http://gw.api.taobao.com/router/rest";
	private final  static String APP_KEY="23628778";
	private final  static String APP_SECRET="aaf139d449d87a60291e0fedea05057d";
	
	/**
	 * 注册验证码短信发送
	 * @return 
	 */

	public  static String send_vertified_code(String vertifed_code ,String phonenumber,String appname) {

		TaobaoClient client = new DefaultTaobaoClient(SMS_URL, APP_KEY,
				APP_SECRET);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("");
		req.setSmsType("normal");
		req.setSmsFreeSignName("客户通知");
		req.setSmsParamString("{number:'"+vertifed_code+"',appname:'"+appname+"'}");
		req.setRecNum(phonenumber);
		req.setSmsTemplateCode("SMS_46370188");
		AlibabaAliqinFcSmsNumSendResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rsp.getBody();
	}

	public  static String send_vertified_code_find_paasword(String vertifed_code ,String phonenumber,String appname) {
		TaobaoClient client = new DefaultTaobaoClient(SMS_URL, APP_KEY,
				APP_SECRET);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("");
		req.setSmsType("normal");
		req.setSmsFreeSignName("客户通知");
		req.setSmsParamString("{code:'"+vertifed_code+"',appname:'"+phonenumber+"',name:'"+appname+"'}");
		req.setRecNum(phonenumber);
		req.setSmsTemplateCode("SMS_45585155");
		AlibabaAliqinFcSmsNumSendResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rsp.getBody();
	}



	/**
	 * 随机数方法
	 */
	public static String  genRandomMath (int charCount) {
		// TODO Auto-generated method stub
		   String charValue = "";
	        for (int i = 0; i < charCount; i++) {
	            char c = (char) (randomInt(0, 10) + '0');
	            charValue += String.valueOf(c);
	        }
	        return charValue;
	}
	 public static int randomInt(int from, int to) {
	        Random r = new Random();
	        return from + r.nextInt(to - from);
	    }


	public static void main(String[] args) {

		SMStools.send_vertified_code("123","18268176350","ssd");
	}


}
